/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.wizards;

import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  */
public abstract class PerforceWizardPage extends WizardPage
{
	protected IProject project;

	/**
	  * Constructor for PerforceWizardPage.
	  * @param pageName
	  */
	protected PerforceWizardPage(String pageName)
	{
		super(pageName);
	}

	/**
	  * Constructor for PerforceWizardPage.
	  * @param pageName
	  * @param title
	  * @param titleImage
	  */
	protected PerforceWizardPage(String pageName,
	                             String title,
	                             ImageDescriptor titleImage)
	{
		super(pageName, title, titleImage);
	}
	
	/**
	  * Gets the project.
	  * @return Returns a IProject
	  */
	public IProject getProject()
	{
		return project;
	}

	/**
	  * Sets the project.
	  * @param project The project to set
	  */
	public void setProject(IProject project)
	{
		this.project = project;
	}

	/**
	  * Utility method that creates a Label instance and sets the default
	  * Layout data.
	  */
	protected static Label createLabel(Composite parent, String labelText)
	{
		Policy.assertNotNull(parent);
		Policy.assertNotNull(labelText);		
		
		Label result = new Label(parent, SWT.WRAP);
		result.setText(labelText);
		
		return result;
	}	
	protected static Label createCenteredLabel(Composite parent)
	{
		Policy.assertNotNull(parent);
		
		
		Label result = new Label(parent, SWT.WRAP|SWT.CENTER);
		
		return result;
	}	
	protected static Label createMessage(Composite parent, String message)
	{
		Policy.assertNotNull(parent);
		Policy.assertNotNull(message);		
		
		Label msgWidget = new Label(parent, SWT.WRAP);
		msgWidget.setText(message);
		GridData gridData = new GridData();
		gridData.grabExcessVerticalSpace=true;
		gridData.widthHint = IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH;
		msgWidget.setLayoutData(gridData);
		
		return msgWidget;
	}	

	/**
	 * Utility method to create a combo box
	 * 
	 * @param parent  the parent of the combo box
	 * @return the created combo
	 */
	protected Combo createCombo(Composite parent)
	{
		Combo combo = new Combo(parent, SWT.READ_ONLY);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		combo.setLayoutData(data);
		return combo;
	}

	protected static Text createText(Composite parent)
	{
		return createText(parent, 0, 0);
	}

	protected static Text createText(Composite parent, int addStyle, int removeStyle)
	{
		Policy.assertNotNull(parent);
		
		Text result = new Text(parent, (SWT.SINGLE | SWT.BORDER | addStyle) & ~removeStyle);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.widthHint = 350;
		data.heightHint = 50;
		result.setLayoutData(data);
		
		return result;
	}
	protected CLabel createIcon(Composite parent,String imageId)
	{
		CLabel icon = new CLabel(parent,SWT.DEFAULT);
		ImageDescriptor imageDescriptor = PerforceUIPlugin.getPlugin().getImageDescriptor(imageId);
		
		Image img = imageDescriptor.createImage();

		icon.setImage(img);
		icon.computeSize(64,64);
		return icon;
	}

	/** Creates composite control and sets the default layout data.
	  * 
	  * <br>This code is directly taken from 
	  * <code>org.eclipse.team.internal.ccvs.ui.wizards.CVSWizardPage</code>.
	  * 
	  * @param parent the parent of the new composite
	  * @param numColumns the number of columns for the new composite
	  * @return the newly-created coposite
	  */
	protected Composite createComposite(Composite parent, int numColumns) 
	{
		Composite composite = new Composite(parent, SWT.NULL);
	
		// GridLayout
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		composite.setLayout(layout);
	
		// GridData
		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);
		return composite;
	}

	/** Create a text field specific for this application
	  * 
	  * <br>This code is directly taken from 
	  * <code>org.eclipse.team.internal.ccvs.ui.wizards.CVSWizardPage</code>.
	  *
	  * @param parent  the parent of the new text field
	  * @return the new text field
	  */
	protected Text createTextField(Composite parent) 
	{
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.verticalAlignment = GridData.CENTER;
		data.grabExcessVerticalSpace = false;
		data.grabExcessHorizontalSpace = true;
		data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		text.setLayoutData(data);
		return text;
	}	
	
	protected Button createButton(Composite parent, String label) 
	{
		Button button = new Button(parent, SWT.PUSH | SWT.BORDER);
		button.setText(label);
	
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END);
		button.setLayoutData(data);
		
		return button;
	}

}
